import { defineStore } from 'pinia'
import { getUserInfo } from "../api";

export const useIndexStore = defineStore('index', {
    state: () => {
        return {
            newDialogEvent:false,
            needLoginEvent:false,
            loginSuccessEvent:false,
            userInfo:{},

            replyList:[],
            replyMap:{
            },
            requestId:"",
            question:"",
            preQuestion:"",
            answerMessage:""
        }
    },
    actions: {
        sendNewDialogEvent(){
            this.newDialogEvent = !this.newDialogEvent;
        },
        sendNeedLoginEvent(){
            this.needLoginEvent = true;
        },
        sendNoNeedLoginEvent(){
            this.needLoginEvent = false;
        },
        getUserInfo(){
            return new Promise((resolve,reject)=>{
                getUserInfo().then(res => {
                    if(res.code === 0){
                        this.userInfo = res.data;
                        this.loginSuccessEvent=true;
                    }else{
                        localStorage.setItem("TOKEN",null);
                        localStorage.removeItem("TOKEN")
                    }
                    resolve(res)
                }).catch(res=>{
                    reject(res);
                });
            })
        },
        appendAnswerMessage(sms,requestId){

            // if(this.requestId === requestId){
            //     this.answerMessage = this.answerMessage + sms;
            // }else{
            //     this.requestId = requestId
            //     this.answerMessage = sms;
            // }
            sms = sms.replace(/\n/g,"<br/>");
            sms = sms.replace(/\s/g," ");
            if(requestId === -1){
                this.replyMap["abnormal"]={
                    answerMessage : sms
                }
            }else{
                this.replyMap[requestId].answerMessage =  this.replyMap[requestId].answerMessage + sms;
            }
        }
    }
})
